package com.yb.module.model.service.impl;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yb.framework.core.base.BaseMapper;
import com.yb.framework.core.base.BaseServiceImpl;
import com.yb.framework.core.utils.SecurityUtil;
import com.yb.framework.core.utils.SnowflakeIdWorker;
import com.yb.framework.neo4j.service.Neo4jService;
import com.yb.module.model.domain.ModelShape;
import com.yb.module.model.mapper.ModelShapeMapper;
import com.yb.module.model.mapper.ModelShapeParamMapper;
import com.yb.module.model.service.ModelShapeService;

@Service
public class ModelShapeServiceImpl extends BaseServiceImpl<ModelShape> implements ModelShapeService {

	@Autowired
	private Neo4jService neo4jService;
	@Autowired
	private ModelShapeMapper modelShapeMapper;
	@Autowired
	private ModelShapeParamMapper modelShapeParamMapper;
	
	@Override
	protected BaseMapper<ModelShape> getMapper() {
		return modelShapeMapper;
	}
	
	@Override
	public int insertShape(ModelShape modelShape) {
		// TODO Auto-generated method stub
		
		long id = SnowflakeIdWorker.getInstance().getId();
		neo4jService.createNode("shape", String.valueOf(id), modelShape.getName());
		
		modelShape.setId(id);
		modelShape.setCreateBy(SecurityUtil.getUsername());
		modelShape.setCreateTime(new Date());
		return modelShapeMapper.insert(modelShape);
	}

	@Override
	public int updateShape(ModelShape modelShape) {
		// TODO Auto-generated method stub
		
		neo4jService.updateNode("shape", String.valueOf(modelShape.getId()), modelShape.getName());
		
		modelShape.setUpdateBy(SecurityUtil.getUsername());
		modelShape.setUpdateTime(new Date());
		return modelShapeMapper.update(modelShape);
	}
	
	@Override
	public int deleteShape(Long[] ids) {
		// TODO Auto-generated method stub
		for (Long id : ids) {
			neo4jService.deleteNode("shape", String.valueOf(id));
		}
		
		for (Long id : ids) {
			modelShapeParamMapper.deleteByShapeId(id);
		}
		return modelShapeMapper.deleteListByIds(ids);
	}
}